package com.example.supernote.ui.manager

import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.example.supernote.ui.note.NoteViewModel
import kotlinx.coroutines.launch

@Composable
fun ManagerRoute(
    noteViewModel: NoteViewModel,
    managerViewModel: ManagerViewModel,
    navigateToNote: () -> Unit,
    isExpandedScreen: Boolean,
    openDrawer: () -> Unit,
    snackbarHostState: SnackbarHostState = remember { SnackbarHostState() }
) {
    val coroutineScope = rememberCoroutineScope()
    val uiState: ManagerUiState by managerViewModel.uiState.collectAsStateWithLifecycle()
    ManagerScreen(
        uiState = uiState,
        search = { managerViewModel.search(it) },
        deleteFile = {
            coroutineScope.launch {
                managerViewModel.deleteFile(it)
            }
        },
        sortFile = { managerViewModel.sortFile() },
        editFile = {
            coroutineScope.launch {
                noteViewModel.fetchFile(it.property!!.id)
                navigateToNote()
            }
        },
        exportFile = { managerViewModel.exportFile(it) },
        createFolder = { managerViewModel.createFolder(it) },
        createFile = { template, name ->
            coroutineScope.launch {
                managerViewModel.createFile(template, name)
            }
        },
        generateNoteTemplateByPrompt = { update, otherInfos ->
            coroutineScope.launch {
                managerViewModel.generateNoteTemplateByPrompt(update, otherInfos)
            }
        },
        forward = { managerViewModel.forward(it) },
        backward = { managerViewModel.backward() },
        preClassified = { unClassified, systemPrompt, update ->
            coroutineScope.launch {
                managerViewModel.preClassified(
                    unClassified,
                    systemPrompt,
                    update
                )
            }
        },
        autoClassified = { coroutineScope.launch { managerViewModel.autoClassified() } },
        moveFilePath = { path, file ->
            coroutineScope.launch {
                managerViewModel.moveFilePath(
                    path,
                    file
                )
            }
        },
        isExpandedScreen = isExpandedScreen,
        openDrawer = openDrawer,
        snackbarHostState = snackbarHostState
    )
}
